package com.qf.service.impl;

import com.qf.mapper.CartDao;
import com.qf.mapper.CartDaoImpl;
import com.qf.mapper.GoodsDao;
import com.qf.mapper.GoodsDaoImpl;
import com.qf.pojo.Cart;
import com.qf.pojo.Goods;
import com.qf.service.CartService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CartServiceImpl implements CartService {


    @Override
    public boolean createCart(Integer uid, Integer pid) {
//        根据用户id和商品id到数据库中查询是否已经有这个商品的数据
        CartDao cartDao = new CartDaoImpl();
        Cart cart = cartDao.findCartByUidPid(uid, pid);

        GoodsDao goodsDao = new GoodsDaoImpl();
        Goods goods = goodsDao.findById(pid);

//        判断是否能能查询到这个购物项的数据
        if (cart != null){
            cart.setNum(cart.getNum() + 1);
            cart.setGoods(goods);
            cart.setMoney(cart.getNum()*goods.getPrice());
            cartDao.updateCart(cart);
        }else {
             cart = new Cart();
            cart.setUid(uid);
                cart.setPid(pid);
                cart.setNum(1);
                cart.setGoods(goods);
                cart.setMoney(cart.getNum()*goods.getPrice());

                cartDao.insertCart(cart);
        }

        return true;

    }

    @Override
    public List<Cart> findCartListByUid(Integer uid) {
        CartDao cartDao = new CartDaoImpl();
        List<Cart> list = cartDao.findCartListByUid(uid);

        GoodsDao goodsDao = new GoodsDaoImpl();
        List<Goods> goodsList = goodsDao.findGoodALl();

        //优化方法
        //将数据量多的集合放入Map<String,Goods>中
        //遍历一个集合，在遍历的过程中，直接从map中get数据
        Map<String,Goods> goodsMap = new HashMap<String, Goods>();
            if (list != null){
                for (Goods goods : goodsList) {
                    goodsMap.put(String.valueOf(goods.getId()), goods);
                }
            }
            if (list != null){
                for (Cart cart : list) {
                    Goods goods = goodsMap.get(String.valueOf(cart.getPid()));
                    if (goods!=null){
                        cart.setGoods(goods);
                    }
                }
            }
//        if (list != null){
//            //购物车集合
//            for (Cart cart : list) {
//                //商品集合
//                for (Goods goods : goodsList) {
//                    if (cart.getPid() == goods.getId()){
//                        cart.setGoods(goods);
//                    }
//                }
//            }
//        }
        return list;
    }

    @Override
    public void updateCart(Integer cid, Integer num) {
        //根据cid 到数据库中查询这个购物项的数据
        CartDao cartDao = new CartDaoImpl();
        Cart cart = cartDao.findById(cid);
        //根据插叙到的购物项的  商品id 到商品表中查询对应的商品数据
        GoodsDao goodsDao = new GoodsDaoImpl();
        Goods goods = goodsDao.findById(cart.getPid());
        // 重新计算总价
        cart.setNum(num);
        cart.setGoods(goods);
        cart.setMoney(goods.getPrice()*num);
        cartDao.updateCart(cart);
    }

    @Override
    public void deleteById(Integer cid) {
        CartDao cartDao = new CartDaoImpl();
        cartDao.deleteById(cid);
    }


}
